#include "networkessential.h"
#include "common.h"
#include "messagetype.h"
#include "messageCiphertext.h"

messageCiphertext::messageCiphertext(const std::string &groupname, 
									 c_asgka_element ciphertext1, 
									 c_asgka_element ciphertext2, 
									 c_asgka_element ciphertext3)
:m_groupname(groupname),
 messageBase(NET_MESSAGE_CIPHERTEXT)
{
	asgka_malloc(m_ciphertext1);
	asgka_malloc(m_ciphertext2);
	asgka_malloc(m_ciphertext3);

	strcpy_s(m_ciphertext1, LENGTH, ciphertext1);
	strcpy_s(m_ciphertext2, LENGTH, ciphertext2);
	strcpy_s(m_ciphertext3, LENGTH, ciphertext3);
}

messageCiphertext::~messageCiphertext()
{
	asgka_free(m_ciphertext1);
	asgka_free(m_ciphertext2);
	asgka_free(m_ciphertext3);
}

void messageCiphertext::generateMessage(std::string &buf) const
{
	// Message content:
	// messagetype message_len groupname_len groupname 
	// ciphertext_len ciphertext1 ciphertext_len ciphertext2 ciphertext_len ciphertext3

	unsigned int messagetype = m_messagetype;
	int message_len;
	int groupname_len  = m_groupname.size();
	int ciphertext_len = LENGTH;
	int temp;

	message_len = groupname_len +  3 * ciphertext_len + 4 * IntLength;

	buf.append(FourBytesToNetChar(messagetype), IntLength);
	buf.append(FourBytesToNetChar(message_len), IntLength);
	buf.append(FourBytesToNetChar(groupname_len), IntLength);
	buf.append(m_groupname);
	temp = ciphertext_len;
	buf.append(FourBytesToNetChar(temp), IntLength);
	buf.append(m_ciphertext1, LENGTH);
	temp = ciphertext_len;
	buf.append(FourBytesToNetChar(temp), IntLength);
	buf.append(m_ciphertext2, LENGTH);
	temp = ciphertext_len;
	buf.append(FourBytesToNetChar(temp), IntLength);
	buf.append(m_ciphertext3, LENGTH);
}
